package jp.co.neosystem.s2wg.service.flex.impl;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;

import jp.co.neosystem.s2wg.dto.flex.ArgsExportXlsWorkSheet;
import jp.co.neosystem.s2wg.dto.flex.DispatchFile;
import jp.co.neosystem.s2wg.dto.flex.ResultExportXlsWorkSheet;
import jp.co.neosystem.s2wg.service.flex.ExportWorkSheetService;
import jp.co.neosystem.s2wg.util.IOUtil;
import jp.co.neosystem.s2wg.util.MessageCode;
import jp.co.neosystem.s2wg.util.StatusFactory;

import org.seasar.flex2.rpc.remoting.service.annotation.RemotingService;

@RemotingService
public class ExportWorkSheetServiceImpl implements ExportWorkSheetService {

	@Override
	public ResultExportXlsWorkSheet exportWorkSheet(
			final ArgsExportXlsWorkSheet args, final boolean isTest) {

		final ResultExportXlsWorkSheet ret = new ResultExportXlsWorkSheet();

		ByteArrayOutputStream bos = null;
		FileInputStream fis = null;
		try {
			fis = new FileInputStream(this.getClass().getClassLoader()
					.getResource("").getFile());
			bos = new ByteArrayOutputStream();
			final byte[] buffer = new byte[2048];
			int size = -1;

			while ((size = fis.read(buffer)) != -1) {
				bos.write(buffer, 0, size);
			}
			final DispatchFile file = new DispatchFile();
			file.setData(bos.toByteArray());
			file.setName("インターフェース定義書");
			ret.setXlsFile(file);
		} catch (final IOException e) {
			e.printStackTrace();
		} finally {
			IOUtil.close(bos);
			IOUtil.close(fis);
		}
		ret.setStatus(StatusFactory.getStatus(MessageCode.WORKSHEET_EXPORT_OK));
		return ret;
	}

}
